package com.ruoyi.mobile.api;


import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import static cn.hutool.crypto.SecureUtil.sha1;

/**
 * 校验使用
 *
 * @author gk
 * @date 2023-06-26
 */
@Api(tags = "校验使用")
@RestController
@RequestMapping
public class WXChatController {

    @RequestMapping("/app/checkSign")
    public void wechatCheckSign(HttpServletRequest request, HttpServletResponse response) throws IOException {

        String token = "slj_weixin_token"; //填写自己在微信公众平台中设置的token

        // 微信加密签名
        String signature = request.getParameter("signature");
        // 随机字符串
        String echostr = request.getParameter("echostr");
        // 时间戳
        String timestamp = request.getParameter("timestamp");
        // 随机数
        String nonce = request.getParameter("nonce");
        String[] str = { token, timestamp, nonce };
        // 字典排序
        Arrays.sort(str);
        String bigStr = str[0] + str[1] + str[2];
        // SHA1加密
        String digest = sha1(bigStr);
        // 确认请求来至微信
        if (digest.equals(signature)) {
            response.getWriter().print(echostr);
        }
    }
}
